package cn.itcast.zuoyebang.view;



import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import java.util.Objects;

import cn.itcast.zuoyebang.R;

public class ViewPagerIndicator extends LinearLayout implements View.OnClickListener {

    private int mCount;//总页面数量
    private int mCurrent;//当前页码
    private ViewPagerIndicatorOnclickListener mListener;

    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setCount(int count) {
        mCount = count;
    }

    public void setCurrent(int curr) {
        removeAllViews();
        for (int i=0; i<mCount; ++i){
            ImageView circle = new ImageView(getContext());//new一个圆点
            //如果是对应的圆点，显示蓝色，否则显示灰色
            if (curr == i) {
                circle.setImageResource(R.drawable.indicator_on);
            } else {
                circle.setImageResource(R.drawable.indicator_off);
            }
            circle.setPadding(5,0,5,0);
            circle.setTag(i);//设置一个标签
            circle.setOnClickListener(this);//设置监听事件
            addView(circle);//增加一个显示的圆点
        }
    }

    public void setOnClickListener(ViewPagerIndicatorOnclickListener listener){
        mListener = listener;
    }

    @Override
    public void onClick(View view) {
        Integer i = (Integer)view.getTag();//取得设置的标签
        mListener.onClick(i);

    }
}
